package demo.practice;

import java.util.Stack;

public class PPattern {

    public static void main(String[] args) {
        PPattern pPattern= new PPattern();
        String pattern = pPattern.getPattern("1000000");
        System.out.println(pattern);
    }
    public String getPattern(String num){
        String before=num.split("\\.")[0];
        String behind=num.contains(".") ? "." +  num.split("\\.")[1]:"";

        Stack<String> stack= new Stack<>();
        while (before.length()>3){
            stack.add(before.substring(before.length()-3));
            before=before.substring(0,before.length()-3);
        }

        StringBuilder sb= new StringBuilder();
        if(before.length()>0){
            sb.append(before);
        }
        while (stack.size()>0){
            sb.append(",").append(stack.pop());
        }
        return sb.toString()+behind;
    }
}
